package com.feidian.helper.adapter;

/**
 * @Author WangCheng
 * @version 1.0
 * @category 2015.9.20 15:57
 * @category用作XListView的适配器，
 * @category将News信息加载到XListView之中
 */

import java.util.List;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.feidian.helper.R;
import com.feidian.helper.bean.Item;
import com.feidian.helper.ui.widget.RatingBar;

public class XListViewAdapter extends BaseAdapter{
    private Context mContext = null;
    private List<Item> newsList;
	public XListViewAdapter(Context context,List<Item> newsList) {
		// TODO Auto-generated constructor stub
		this.mContext = context;
		this.newsList = newsList;
	}
    @Override
    public int getCount(){
    	return this.newsList.size();
    }
    @Override
    public long getItemId(int position){
    	return 0;
    }
    @Override
    public Object getItem(int position){
    	return newsList.size();
    }

    @Override 
    public View getView(int position,View convertView,ViewGroup parent){
    	
    	ViewHolder holder = null;
		if (convertView == null) {
			holder = new ViewHolder();
			LayoutInflater inflater = LayoutInflater.from(mContext);
			convertView = inflater.inflate(R.layout.news_item, null);
			holder.nameTv = (TextView) convertView.findViewById(R.id.name);
			holder.titleTv = (TextView) convertView.findViewById(R.id.title);
			holder.pastTimeTv = (TextView) convertView
					.findViewById(R.id.past_time);
			holder.contentTv = (TextView) convertView
					.findViewById(R.id.content);
			holder.priceTv = (TextView) convertView.findViewById(R.id.price);
			holder.endlineTv = (TextView) convertView
					.findViewById(R.id.endline);
			holder.honestyRatingBar = (RatingBar) convertView
					.findViewById(R.id.ratingBar);
			holder.stateTv=(TextView)convertView.findViewById(R.id.state);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		holder.nameTv.setText(newsList.get(position).getName());
		holder.titleTv.setText(newsList.get(position).getTitle());
		holder.pastTimeTv.setText(newsList.get(position).getPastTime());
		holder.contentTv.setText(newsList.get(position).getContent());
		if(newsList.get(position).getPrice()!=null&&!newsList.get(position).getPrice().equals("null"))
		holder.priceTv.setText(newsList.get(position).getPrice()+"元");
		else holder.priceTv.setText("0元");
		holder.endlineTv.setText(newsList.get(position).getEndline());
		if(newsList.get(position).getHonesty()==null||newsList.get(position).getHonesty().equals("null"))
		{
			Log.e("newsList.get(position).getHonesty()=null",".............");
			holder.honestyRatingBar.setStar(5);
		}
		else
		{
			Log.e("newsList.get(position).getHonesty()=",newsList.get(position).getHonesty());
			/*holder.honestyRatingBar.setStar(Float.parseFloat(newsList.get(
					position).getHonesty()));
			*/
			int rating =(int)Float.parseFloat(newsList.get(position).getHonesty());
			holder.honestyRatingBar.setStar(rating);
		}
		
		if(newsList.get(position).getState().equals("0"))
		{
			holder.stateTv.setText("未接单");
		}
		else if(newsList.get(position).getState().equals("1"))
		{
			holder.stateTv.setText("正在处理中");
		}
		else if(newsList.get(position).getState().equals("2"))
		{
			holder.stateTv.setText("已经处理");
		}
		else
		{
			holder.stateTv.setText("出错");
		}
		return convertView;
    }
    static class ViewHolder {
		TextView nameTv;// 名字
		TextView titleTv;// 类别
		TextView pastTimeTv;// 离发布的时间差
		TextView contentTv;// 求助内容
		TextView priceTv;// 求助金额
		TextView endlineTv;// 截止日期
		com.feidian.helper.ui.widget.RatingBar honestyRatingBar;// 发帖信用度
		TextView stateTv;
	}
}
